<template>
  <div>
    <el-button :disabled="!category3Id" type="primary" icon="el-icon-plus" @click="addSpuInfo">添加SPU</el-button>
    <el-table v-loading="loading" style="margin:10px 0" border :data="spuList">
      <el-table-column width="80" align="center" label="序号" type="index" />
      <el-table-column label="SPU名称" prop="spuName" />
      <el-table-column label="SPU描述" prop="description" />
      <el-table-column label="操作">
        <template v-slot="{row}">
          <el-tooltip content="添加SKU" placement="top">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="addSkuInfo(row)" />
          </el-tooltip>
          <el-tooltip content="更新SPU" placement="top">
            <el-button type="warning" icon="el-icon-edit" size="mini" @click="editSpuInfo(row)" />
          </el-tooltip>
          <el-tooltip content="查看SKU列表" placement="top">
            <el-button
              type="info"
              icon="el-icon-info"
              size="mini"
              @click="getSkuList(row)"
            />
          </el-tooltip>
          <el-popconfirm confirm-button-text="确定" cancel-button-text="取消" icon="el-icon-info" icon-color="red" title="您真的要删除此项SPU吗？" @onConfirm="deleteSpuInfo(row,$index)">
            <el-tooltip slot="reference" content="删除SPU" placement="top">
              <el-button type="danger" icon="el-icon-delete" size="mini" />
            </el-tooltip>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      style="text-align: center"
      :current-page="currentPage"
      :pager-count="7"
      :page-sizes="[3, 5, 10]"
      :page-size="limit"
      layout="prev, pager, next, jumper,->,total, sizes"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 查看的sku数据列表 -->
    <el-dialog title="SKU列表数据" :visible.sync="dialogTableVisible">
      <el-table border :data="skuList">
        <el-table-column label="名称" prop="skuName" />
        <el-table-column label="价格" prop="price" />
        <el-table-column label="描述" prop="skuDesc" />
        <el-table-column label="默认图片">
          <template v-slot="{ row }">
            <img :src="row.skuDefaultImg" alt="" style="width:100px;height:100px">
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import {
  reqGetSpuList,
  reqDeleteSpuInfo // 删除spu
} from '@/api/product/spu'
import { reqGetSkuList } from '@/api/product/sku'
import { mapMutations, mapState } from 'vuex'
export default {
  name: 'SpuList',
  data() {
    return {
      loading: false,
      currentPage: 1,
      total: 0,
      limit: 3,
      spuList: [],
      dialogTableVisible: false,
      skuList: []
    }
  },
  computed: {
    ...mapState('category', ['category3Id'])
  },
  watch: {
    category3Id: {
      immediate: true,
      handler() {
        const { category3Id } = this
        if (!category3Id) {
          this.spuList = []
          this.currentPage = 1
          this.limit = 3
          this.total = 0
          return // 阻止代码项下执行
        }
        this.getSpuList()
      }
    }
  },
  methods: {
    ...mapMutations('spu', ['SAVE_SPU_ID']),
    // 6. 查看所有的sku
    async getSkuList(row) {
      // 显示对话框
      this.dialogTableVisible = true
      const result = await reqGetSkuList(row.id)
      if (result.code === 200) {
        this.skuList = result.data
      } else {
        this.$message({
          type: 'error',
          message: '获取sku失败'
        })
      }
    },
    // 5.添加sku
    addSkuInfo(row) {
      this.$emit('update:visible', 3)
      this.SAVE_SPU_ID(row)
    },
    // 4.删除spu
    async deleteSpuInfo(row, index) {
      const res = await reqDeleteSpuInfo(row.id)
      if (res.code === 200) {
        this.getSpuList()
        this.$message.success('删除spu成功')
      } else {
        this.$message.error('删除spu失败')
      }
    },
    // 3.添加spu
    addSpuInfo() {
      this.$emit('update:visible', 2)
      this.$emit('setSpuInfo', {
        spuImageList: [],
        spuSaleAttrList: []
      })
    },
    // 2.修改spu
    editSpuInfo(row) {
      this.$emit('update:visible', 2)
      // 传值
      this.$emit('setSpuInfo', {
        ...row,
        spuImageList: [],
        spuSaleAttrList: []
      })
    },
    // 1.获取spu数据
    async getSpuList() {
      const { currentPage: page, limit, category3Id } = this
      this.loading = true
      const res = await reqGetSpuList({ page, limit, category3Id })
      if (res.code === 200) {
        this.spuList = res.data.records
        this.total = res.data.total
        this.loading = false
      }
    },
    // 分页相关方法
    handleSizeChange(val) {
      this.limit = val
      this.getSpuList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getSpuList()
    }
  }
}
</script>

<style>

</style>
